import 'package:flutter/material.dart';

import '../extension/index.dart';
import 'common_log.dart';

///json的解析工具
class CommonJson {
  ///获取某类型的列表
  static Future<List<T>> getList<T>(dynamic json, T Function(dynamic json) f, {String keyName = ''}) async {
    List? list;
    if (keyName.notEmpty) {
      list = json[keyName];
    } else {
      list = json;
    }

    if (list == null) return Future.value(<T>[]);
    List<T> result = list.map((e) => f(e)).toList();
    return result;
  }

  ///处理json
  static reduceJson(VoidCallback reduceJson, Function(dynamic e) error) {
    try {
      reduceJson();
    } catch (e) {
      Log.log("json 解析失败：${e.toString()}");
      error(e);
    }
  }
}
